package com.gaojinqi.order.message;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 接受MQ消息
 */
@Slf4j
@Component
public class MqReceiver {

    // 第一种方式：需要手动创建queue
    //@RabbitListener(queues = "myQueue")
    // 第二种方式，自动创建queue
    //@RabbitListener(queuesToDeclare = @Queue("myQueue"))
    //第三种方式：自动创建，queue与exchange绑定
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("myQueue"),
            exchange = @Exchange("myExchange")
    ))
    public void process(String message) {
        log.info("MqReceiver：{}", message);
    }

    /**
     * 假设场景一：提供订单服务，与其他服务对接
     */
    /**
     * 数码供应商 接受消息
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("myOrder"),
            key = "computer",
            value = @Queue("computerOrder")
    ))
    public void processComputer(String message) {
        log.info("computer MqReceiver：{}", message);
    }

    /**
     * 数码供应商 接受消息
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("myOrder"),
            key = "fruit",
            value = @Queue("fruitOrder")
    ))
    public void processFruit(String message) {
        log.info("fruit MqReceiver：{}", message);
    }
}
